﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
[assembly:  CLSCompliant(true)]

namespace Eudaemon.ProeAssist.Core
{
    /// <summary>
    /// Record, broswe paths in a collection.
    /// </summary>
    public class DirectoryWalker
    {
        /// <summary>
        /// Move current index points to the previous path string if any
        /// </summary>
        public void MovePrevious()
        {
            if (IsIndexValid(_currentPathIndex - 1))
                --_currentPathIndex;
        }

        /// <summary>
        /// Move current index points to the next path string if any
        /// </summary>
        public void MoveNext()
        {
            if (IsIndexValid(_currentPathIndex + 1))
                ++_currentPathIndex;
        }

         /// <summary>
        /// Add a parent path and make current index points to it if any
        /// </summary>
       public void UpLevel()
        {
            if (IsPathRoot(CurrentPath))
                return;
            Add(Directory.GetParent(CurrentPath).ToString());
        }

         /// <summary>
        /// Add a new path at current index and remove all after it
        /// </summary>
       public void Add(string path)
        {
            if (path == null || path == this.CurrentPath)
                return;
           if(!Directory.Exists(path))
               return;
           Insert(path);
           Shrink();
        }

        /// <summary>
        /// current path string
        /// </summary>
        public string CurrentPath
        {
            get
            {
                if (_paths.Count > 0 && IsIndexValid(_currentPathIndex))
                    return _paths[_currentPathIndex];
                else
                    return null;
            }
            set
            {
                this.Add(value);
            }
        }

        /// <summary>
        /// Return the upper litmit of paths' number, readonly
        /// </summary>
        public int MaxPathNumber
        {
            get
            {
                return _maxPathNumber;
            }
        }

        public bool UpwardEnabled()
        {
            if (_paths.Count <= 0)
                return false;
            return !IsPathRoot(this.CurrentPath);
        }

        public bool ForwardEnabled()
        {
            if (_paths.Count <= 0)
                return false;
            return !IsCurrentPathLast();
        }

        public bool BackwardEnabled()
        {
            if (_paths.Count <= 0)
                return false;
            return !IsCurrentPathFirst();
        }

        #region Protected & Private
        /// <summary>
        /// Remove all paths after current index.
        /// When the current index doesn't point to the last path, simply shrink it!
        /// </summary>
        private void Shrink()
        {
            if (!IsCurrentPathLast())
                _paths.RemoveRange(_currentPathIndex + 1, _paths.Count - _currentPathIndex - 1);
        }
        
        private bool IsIndexValid(int index)
        {
            return (index < 0 || index >= _paths.Count) ? false : true;
        }

        private static bool IsPathRoot(string path)
        {
            return (Directory.GetParent(path) == null) ? true : false;
        }

        private bool IsCurrentPathFirst()
        {
            return (_currentPathIndex == 0) ? true : false;
        }

        private bool IsCurrentPathLast()
        {
            return (_currentPathIndex == _paths.Count-1) ? true : false;
        }

        /// <summary>
        /// Insert a new path at current index
        /// </summary>
        private void Insert(string path)
        {
            ++_currentPathIndex;
            _paths.Insert(_currentPathIndex, path);
            LitmitSize();
        }
        
        /// <summary>
        /// Make sure number of paths
        /// </summary>
        private void LitmitSize()
        {
            if (_paths.Count > _maxPathNumber)
            {
                _paths.RemoveAt(0);
                --_currentPathIndex;
            }
        }

        private int _currentPathIndex = -1;
        private int _maxPathNumber = 16;
        private List<string> _paths = new List<string>();

        #endregion

    }
}
